{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.preprocessing import OneHotEncoder,Binarizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression,LinearRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,mean_squared_error,recall_score,roc_auc_score\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "import joblib\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "           0         1         2         3         4         5         6  \\\n0  -0.430783 -1.637356 -2.006212 -1.862426 -1.024706 -0.522941 -0.863171   \n1  -0.162519 -1.988980 -0.722009 -0.787896 -1.024706 -0.522941 -0.863171   \n2  -0.162519 -1.578819 -2.188784  1.361163 -1.024706 -0.522941 -0.863171   \n3  -0.162519 -2.166917 -0.807994 -0.787896 -1.024706 -0.522941 -0.863171   \n4   0.371564 -0.507874 -0.458834 -0.250631 -1.024706 -0.522941 -0.863171   \n..       ...       ...       ...       ...       ...       ...       ...   \n62  4.029806  1.609063  1.103786  0.555266 -1.024706 -0.522941 -0.863171   \n63  4.129551  1.003621  0.113497 -0.384948  0.860016  1.892548 -0.863171   \n64  4.385147  1.255920  0.577607  0.555266 -1.024706  1.892548  1.073572   \n65  4.684443  2.096506  0.625489 -2.668323 -1.024706  1.892548  1.679542   \n66  5.477509  1.300290  0.338384  0.555266  1.004813  1.892548  1.242632   \n\n           7         8  \n0  -1.042157 -0.864467  \n1  -1.042157 -0.864467  \n2   0.342627 -0.155348  \n3  -1.042157 -0.864467  \n4  -1.042157 -0.864467  \n..       ...       ...  \n62 -1.042157 -0.864467  \n63  0.342627 -0.332628  \n64  0.342627  1.262889  \n65  0.342627  0.553770  \n66  0.342627  1.972007  \n\n[67 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n      <th>6</th>\n      <th>7</th>\n      <th>8</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.430783</td>\n      <td>-1.637356</td>\n      <td>-2.006212</td>\n      <td>-1.862426</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-0.162519</td>\n      <td>-1.988980</td>\n      <td>-0.722009</td>\n      <td>-0.787896</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.162519</td>\n      <td>-1.578819</td>\n      <td>-2.188784</td>\n      <td>1.361163</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>0.342627</td>\n      <td>-0.155348</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-0.162519</td>\n      <td>-2.166917</td>\n      <td>-0.807994</td>\n      <td>-0.787896</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.371564</td>\n      <td>-0.507874</td>\n      <td>-0.458834</td>\n      <td>-0.250631</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>62</th>\n      <td>4.029806</td>\n      <td>1.609063</td>\n      <td>1.103786</td>\n      <td>0.555266</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n    </tr>\n    <tr>\n      <th>63</th>\n      <td>4.129551</td>\n      <td>1.003621</td>\n      <td>0.113497</td>\n      <td>-0.384948</td>\n      <td>0.860016</td>\n      <td>1.892548</td>\n      <td>-0.863171</td>\n      <td>0.342627</td>\n      <td>-0.332628</td>\n    </tr>\n    <tr>\n      <th>64</th>\n      <td>4.385147</td>\n      <td>1.255920</td>\n      <td>0.577607</td>\n      <td>0.555266</td>\n      <td>-1.024706</td>\n      <td>1.892548</td>\n      <td>1.073572</td>\n      <td>0.342627</td>\n      <td>1.262889</td>\n    </tr>\n    <tr>\n      <th>65</th>\n      <td>4.684443</td>\n      <td>2.096506</td>\n      <td>0.625489</td>\n      <td>-2.668323</td>\n      <td>-1.024706</td>\n      <td>1.892548</td>\n      <td>1.679542</td>\n      <td>0.342627</td>\n      <td>0.553770</td>\n    </tr>\n    <tr>\n      <th>66</th>\n      <td>5.477509</td>\n      <td>1.300290</td>\n      <td>0.338384</td>\n      <td>0.555266</td>\n      <td>1.004813</td>\n      <td>1.892548</td>\n      <td>1.242632</td>\n      <td>0.342627</td>\n      <td>1.972007</td>\n    </tr>\n  </tbody>\n</table>\n<p>67 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\月考练习算法题 (2)\\\\月考练习算法题\\\\第2套（修改2）\\\\专高6月考-02附件\\\\lpsa.data\",names=[i for i in range(9)])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "           0         1         2         3         4         5         6  \\\n0  -0.430783 -1.637356 -2.006212 -1.862426 -1.024706 -0.522941 -0.863171   \n1  -0.162519 -1.988980 -0.722009 -0.787896 -1.024706 -0.522941 -0.863171   \n2  -0.162519 -1.578819 -2.188784  1.361163 -1.024706 -0.522941 -0.863171   \n3  -0.162519 -2.166917 -0.807994 -0.787896 -1.024706 -0.522941 -0.863171   \n4   0.371564 -0.507874 -0.458834 -0.250631 -1.024706 -0.522941 -0.863171   \n..       ...       ...       ...       ...       ...       ...       ...   \n62  4.029806  1.609063  1.103786  0.555266 -1.024706 -0.522941 -0.863171   \n63  4.129551  1.003621  0.113497 -0.384948  0.860016  1.892548 -0.863171   \n64  4.385147  1.255920  0.577607  0.555266 -1.024706  1.892548  1.073572   \n65  4.684443  2.096506  0.625489 -2.668323 -1.024706  1.892548  1.679542   \n66  5.477509  1.300290  0.338384  0.555266  1.004813  1.892548  1.242632   \n\n           7         8  LabeledPoint  \n0  -1.042157 -0.864467           0.0  \n1  -1.042157 -0.864467           0.0  \n2   0.342627 -0.155348           0.0  \n3  -1.042157 -0.864467           0.0  \n4  -1.042157 -0.864467           0.0  \n..       ...       ...           ...  \n62 -1.042157 -0.864467           1.0  \n63  0.342627 -0.332628           1.0  \n64  0.342627  1.262889           1.0  \n65  0.342627  0.553770           1.0  \n66  0.342627  1.972007           1.0  \n\n[67 rows x 10 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n      <th>6</th>\n      <th>7</th>\n      <th>8</th>\n      <th>LabeledPoint</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.430783</td>\n      <td>-1.637356</td>\n      <td>-2.006212</td>\n      <td>-1.862426</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-0.162519</td>\n      <td>-1.988980</td>\n      <td>-0.722009</td>\n      <td>-0.787896</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.162519</td>\n      <td>-1.578819</td>\n      <td>-2.188784</td>\n      <td>1.361163</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>0.342627</td>\n      <td>-0.155348</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-0.162519</td>\n      <td>-2.166917</td>\n      <td>-0.807994</td>\n      <td>-0.787896</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.371564</td>\n      <td>-0.507874</td>\n      <td>-0.458834</td>\n      <td>-0.250631</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>62</th>\n      <td>4.029806</td>\n      <td>1.609063</td>\n      <td>1.103786</td>\n      <td>0.555266</td>\n      <td>-1.024706</td>\n      <td>-0.522941</td>\n      <td>-0.863171</td>\n      <td>-1.042157</td>\n      <td>-0.864467</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>63</th>\n      <td>4.129551</td>\n      <td>1.003621</td>\n      <td>0.113497</td>\n      <td>-0.384948</td>\n      <td>0.860016</td>\n      <td>1.892548</td>\n      <td>-0.863171</td>\n      <td>0.342627</td>\n      <td>-0.332628</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>64</th>\n      <td>4.385147</td>\n      <td>1.255920</td>\n      <td>0.577607</td>\n      <td>0.555266</td>\n      <td>-1.024706</td>\n      <td>1.892548</td>\n      <td>1.073572</td>\n      <td>0.342627</td>\n      <td>1.262889</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>65</th>\n      <td>4.684443</td>\n      <td>2.096506</td>\n      <td>0.625489</td>\n      <td>-2.668323</td>\n      <td>-1.024706</td>\n      <td>1.892548</td>\n      <td>1.679542</td>\n      <td>0.342627</td>\n      <td>0.553770</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>66</th>\n      <td>5.477509</td>\n      <td>1.300290</td>\n      <td>0.338384</td>\n      <td>0.555266</td>\n      <td>1.004813</td>\n      <td>1.892548</td>\n      <td>1.242632</td>\n      <td>0.342627</td>\n      <td>1.972007</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>67 rows × 10 columns</p>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.iloc[:, 0]>0\n",
    "\n",
    "bina = Binarizer(threshold=2)\n",
    "result = bina.fit_transform(df.iloc[:, 0].values.reshape(-1,1))\n",
    "df[\"LabeledPoint\"] = result\n",
    "# df.iloc[:,[0]]\n",
    "# df.drop(\"lable\" ,axis=1,inplace=True)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "0     0.0\n1     0.0\n2     0.0\n3     0.0\n4     0.0\n     ... \n62    1.0\n63    1.0\n64    1.0\n65    1.0\n66    1.0\nName: LabeledPoint, Length: 67, dtype: float64"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = df.iloc[:,1:-1]\n",
    "y = df[\"LabeledPoint\"]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "62    1.0\n34    1.0\n48    1.0\n7     0.0\n41    1.0\n30    1.0\n54    1.0\n19    0.0\n44    1.0\n42    1.0\n31    1.0\n46    1.0\n13    0.0\n53    1.0\n3     0.0\n17    0.0\n33    1.0\n8     0.0\n47    1.0\n6     0.0\n66    1.0\n65    1.0\n15    0.0\n27    1.0\n26    1.0\n24    1.0\n50    1.0\n11    0.0\n32    1.0\n63    1.0\n49    1.0\n37    1.0\n29    1.0\n43    1.0\n55    1.0\n1     0.0\n21    0.0\n2     0.0\n56    1.0\n39    1.0\n35    1.0\n52    1.0\n23    1.0\n58    1.0\n10    0.0\n22    0.0\n18    0.0\n57    1.0\n38    1.0\n20    0.0\n60    1.0\n14    0.0\n51    1.0\nName: LabeledPoint, dtype: float64"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "DecisionTreeClassifier()",
      "text/html": "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeClassifier()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">DecisionTreeClassifier</label><div class=\"sk-toggleable__content\"><pre>DecisionTreeClassifier()</pre></div></div></div></div></div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 决策树\n",
    "tree = DecisionTreeClassifier(criterion='gini',max_depth=None,splitter='best')\n",
    "tree.fit(X_train,y_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "RandomForestClassifier(max_depth=6)",
      "text/html": "<style>#sk-container-id-3 {color: black;}#sk-container-id-3 pre{padding: 0;}#sk-container-id-3 div.sk-toggleable {background-color: white;}#sk-container-id-3 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-3 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-3 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-3 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-3 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-3 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-3 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-3 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-3 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-3 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-3 div.sk-item {position: relative;z-index: 1;}#sk-container-id-3 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-3 div.sk-item::before, #sk-container-id-3 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-3 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-3 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-3 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-3 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-3 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-3 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-3 div.sk-label-container {text-align: center;}#sk-container-id-3 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-3 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>RandomForestClassifier(max_depth=6)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">RandomForestClassifier</label><div class=\"sk-toggleable__content\"><pre>RandomForestClassifier(max_depth=6)</pre></div></div></div></div></div>"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机森林\n",
    "rf = RandomForestClassifier(n_estimators=100,criterion='gini',max_depth=6)\n",
    "rf.fit(X_train,y_train)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [],
   "source": [
    "parameters1 = {'max_depth':[6,10],'splitter':['best','random'],'min_samples_leaf':[1,2]}\n",
    "parameters2 = {'n_estimators':[50,100],'criterion':['gini','entropy'],'max_depth':[6,10]}\n",
    "\n",
    "tree_model = GridSearchCV(tree,parameters1,cv=5)\n",
    "rf_model = GridSearchCV(rf,parameters2,cv=5)\n",
    "\n",
    "# 训练模型\n",
    "tree_model.fit(X_train,y_train)\n",
    "rf_model.fit(X_train,y_train)\n",
    "# 获取最优模型\n",
    "best_tree = tree_model.best_estimator_\n",
    "best_rf = rf_model.best_estimator_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "0.7708333333333335"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred_tree = best_tree.predict(X_test)\n",
    "y_pred_rf = best_rf.predict(X_test)\n",
    "accuracy_score(y_test,y_pred_tree)\n",
    "recall_score(y_test,y_pred_tree)\n",
    "roc_auc_score(y_test,y_pred_tree)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8333333333333334"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy_score(y_test,y_pred_rf)\n",
    "recall_score(y_test,y_pred_rf)\n",
    "roc_auc_score(y_test,y_pred_rf)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "{'criterion': 'entropy', 'max_depth': 6, 'n_estimators': 100}"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree_model.best_params_\n",
    "rf_model.best_params_\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 0., 1., 0., 1., 1., 1., 0., 1., 1., 0., 1., 1., 0.])"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred1 = best_tree.predict(X_test)\n",
    "y_pred1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "          正常       0.80      0.67      0.73         6\n",
      "          异常       0.78      0.88      0.82         8\n",
      "\n",
      "    accuracy                           0.79        14\n",
      "   macro avg       0.79      0.77      0.78        14\n",
      "weighted avg       0.79      0.79      0.78        14\n",
      "\n"
     ]
    }
   ],
   "source": [
    "result = classification_report(y_test,y_pred1,labels=[0, 1],target_names=['正常', '异常'])\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}